<!DOCTYPE html>
<%@ page import="java.util.*,rubix.model.*,rubix.controller.*,java.text.*,rubix.model.Part" %>
<%@include file ="masterPage.jsp" %>

<%
    if (!department.equals("Sales & Purchasing")) {
        response.sendRedirect("index.jsp");
    }
    DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    String todayDate = df.format(new Date());
    DecimalFormat df2 = new DecimalFormat("#,###,##0.00");
    ArrayList<Supplier> supplierList = SupplierManager.retrieveAll();    
    ArrayList<Item> itemList = ItemManager.retrieveAll();    
    String selectedSupplierId = request.getParameter("supplierID");  
    ArrayList<Item> selectedItemList = ItemManager.retrieveItemList(selectedSupplierId);
    String selectedItemId = request.getParameter("itemID");
    String selectedPartId = request.getParameter("partID");
    String sessionSupplierId = (String)session.getAttribute("supplierID");
    out.println(selectedSupplierId);
    out.println(selectedItemId);
    out.println(selectedPartId);
    out.println(todayDate);
    out.println(request.getParameter("itemQuantity"));
    out.println(sessionSupplierId);
%>

<html>
    <head>
        <title>KMP Fast East Pte Ltd - ERP Application</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
        <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
        <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
        <link rel="stylesheet" href="/resources/demos/style.css" />
        <script>
            $(function() {
                $("#datepicker").datepicker();
            });
            
            var k1 = 0;
            var k2 = 0;
            var countK1 = 0;
            var countK2 = 0
            function calculateSubTotal(itemId) {
                var count = 0;
                var k = 0;
                var q = document.getElementById("quantity"+itemId).value;
                var p = document.getElementById("price"+itemId).value;
                var subPrice = parseFloat(q) * parseFloat(p); 
                document.getElementById("demo"+itemId).innerHTML=subPrice;
                
                if (itemId == 'K1'){
                   countK1 = 1;
                   k1 = document.getElementById("demo"+itemId).innerHTML
                } else {
                   countK1 = 1;
                   k2 = document.getElementById("demo"+itemId).innerHTML
                }
                var totalC = parseFloat(countK1) + parseFloat(countK2);
                var totalA = parseFloat(k1) + parseFloat(k2);
                document.getElementById("noOfItem").innerHTML=totalC;
                document.getElementById("total").innerHTML=totalA;
            }
        </script>
    </head>
    <body>
        <div id="page-wrapper">

            <div class="col-lg-12">
                <div class="panel panel-primary">
                    <div class="panel-heading">    
                        <h1 class="panel-title"><i class="icon-file-text-alt"></i> Create Purchase Order</h1><br>
                        <%--<a href="purchaseOrderDetails.jsp"><button type="button" class="btn btn-danger"><i class="icon-edit"></i> Confirm Order</button></a>--%>
                    </div>

                    <form name="supplierlist" method="post" action="createPurchaseOrder.jsp">
                    <div class="panel-body"> 
                        <div class="table-responsive">
                            <table class="table table-condensed table-hover table-striped tablesorter">                                
                                <thead>
                                <h1>Create Purchase Order</h1>
                                <tr>
                                    <th>Supplier</th>
                                    <th></th>
                                    <th>Date</th>
                                    <th></th>
                                </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td><select name="supplierID" class="form-control">  
                                            <% if(selectedSupplierId != null) { 
                                                session.setAttribute("supplierID",selectedSupplierId);
                                                String day = todayDate.substring(0,2);
                                                String month = todayDate.substring(3,5);
                                                String year = todayDate.substring(6);
                                                String newDate = day + month + year;
                                                session.setAttribute("todayDate",newDate);
                                            } %>
                                            <option>Select a supplier</option>
                                            <% //this part requires further codes 
                                                for(Supplier supplier : supplierList) {
                                                    String supplierId = supplier.getSupplierId();  
                                            %>
                                            <option name="supplierID" selected="" value="<%=supplierId%>"><%=supplierId%></option>
                                            <%  }  %></select></td>
                                        <td><button class="btn btn-success" type="submit">Submit</button></td>
                                        <td><input type="hidden" name="todayDate" value="<%=todayDate%>"><%=todayDate%></td>                                
                                    </tr>
                                    <tr>
                                </tbody>
                            </table>
                        </form>
                        <form name="createPoForm" method="post" action="poController">
                            <table class="table table-bordered table-hover table-striped tablesorter">
                                <thead>
                                    <tr class="success">
                                        <th>S/N -- Click to Add --</th>
                                        <th>Item ID</th>    
                                        <th>Part ID</th>
                                        <th>Unit Price</th>
                                        <th>Quantity</th>
                                        <th>Sub-Total</th>
                                    </tr>
                                </thead>
                                <tbody>
                                <%
                               if(selectedSupplierId != null) {          
                                %>
                                    <tr>
                                        <%
                                            for(int i=1; i<selectedItemList.size()+1; i++) {
                                                Item item = selectedItemList.get(i-1);
                                                String itemId = item.getItemId();
                                                String partId = item.getPartId();
                                                String price = "";
                                        %>
                                        <td><input type="submit" name="checkedItem" value="<%=i%>" class="btn btn-link"></input></td>
                                        <td><%=itemId%><input type="hidden" name="itemID<%=i%>" value="<%=itemId%>"></td>
                                        <td><%=partId%><input type="hidden" name="partID<%=i%>" value="<%=partId%>"></td>
                                        <% if(partId==null) { %>
                                        <td><%=0%></td>
                                        <% } try { 
                                            Part part = PartManager.retrievePart(partId);
                                            price = part.getPartPrice(); 
                                        %>
                                        <td><input type="hidden" name="price" value="<%=price%>" id="price<%=itemId%>"><%=price%></input></td>
                                        <% } catch(NullPointerException e) { %>
                                        <td><%=0%></td> <% }%>
                                        <%--<td><input type="text" id="datepicker" name="orderDate" class="form-control" placeholder="Pick a Date" /></td>
                                        <td><input name="itemPrice" class="form-control" placeholder="Enter Unit Price"></td>--%>
                                        <td><input name="itemQuantity<%=i%>" class="form-control" type="number" placeholder="0" value="5" id="quantity<%=itemId%>" onClick="calculateSubTotal('<%=itemId%>')"></td>
                                        <%--
                                            String quantity = request.getParameter("itemQuantity");
                                            try {         
                                       --%><td><label name="subTotal<%=i%>" id="demo<%=itemId%>"/></td><%-- } 
                                            catch(NullPointerException e) { %>
                                            <td><%=df2.format(5*Double.parseDouble(price))%><input type="hidden" name="subTotal"></td> <% }
                                            catch(NumberFormatException e) { %>
                                                <td><%=0%></td> 
                                            <% }  --%>
                                    </tr>
                                    <% }} else {%> 
                                    <tr>
                                        <td>1</td>
                                        <td>6Y 5911</td>
                                        <td>DISC, PA 7G3129</td>
                                        <td>30</td>
                                        <td>9.52</td>
                                        <td>285.60</td>
                                    </tr>
                                    <tr>
                                        <td>2</td>
                                        <td>6Y 7914</td>
                                        <td>DISC, PA 5K8617 (30)</td>
                                        <td>100</td>
                                        <td>5.00</td>
                                        <td>500.00</td>
                                    </tr>
                                    <tr>
                                        <td>3</td>
                                        <td>6Y 7915</td>
                                        <td>DISC, PA (GREEN) - 2G9908</td>
                                        <td>100</td>
                                        <td>3.60</td>
                                        <td>360.00</td>
                                    </tr>
                                    <tr>
                                        <td>4</td>
                                        <td>SE 4075</td>
                                        <td>DISC, ASBESTOS, 6S3183</td>
                                        <td>100</td>
                                        <td>5.00</td>
                                        <td>500.00</td>
                                    </tr>
                                    <% } %>
                                </tbody>
                            </table>
                            <div class="col-lg-13">
                                <table class="table table-condensed">
                                    <thead>
                                        <tr>
                                            <th class="active" colspan="8">Item Total</th>
                                            <td><label name="noOfItems" id="noOfItem"/></td>
                                        </tr>
                                        <tr>                                     
                                            <th class="active" colspan="8">Tax</th>
                                            <td>0.00</td>
                                        </tr>
                                        <tr>                                                                             
                                            <th class="active" colspan="8">Discount</th>
                                            <td>-0.00</td> 
                                        </tr>
                                        <tr>                                                                             
                                            <th class="danger" colspan="8">Order Total (USD)</th>
                                            <td><label name="totalAmount" id="total"/></td> 
                                        </tr>
                                    </thead>                              
                                </table>
                            </div>
                        </form>
                            <form role="form">                        
                                <textarea class="form-control" rows="3" placeholder="Term and conditions..."></textarea>
                            </form>  
                        </div>
                    </div>
                    
                </div>
            </div>
        </div>
    </body>
</html>